package dwei.core.load.proxy
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	
	internal class LoadText extends LoadAbstract
	{
//============================================================================
//PROPERTY
//============================================================================
		protected var _loader:URLLoader;
//============================================================================
//CONSTRUCTOR
//============================================================================
		public function LoadText()
		{
			super();
			
			_loader = new URLLoader();
			_loader.dataFormat = URLLoaderDataFormat.TEXT;
			_loader.addEventListener(Event.COMPLETE, complete_handler);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, error_handler);
			_loader.addEventListener(ProgressEvent.PROGRESS, progress_handler);
			_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityError_handler);
		}
//============================================================================
//OVERRIDE
//============================================================================
		override public function load(url:String):void
		{
			if (_isLoading) return;
			_isLoading = true;
			_loader.load(new URLRequest(url));
		}
//============================================================================
//HANDLER
//============================================================================
		protected function complete_handler(event:Event):void
		{
			_isLoading = false;
			_data = _loader.data;
			dispatchEvent(new Event("complete"));
		}
		protected function error_handler(event:IOErrorEvent):void
		{
			_isLoading = false;
			_data = event.text;
			dispatchEvent(new Event("error"));
		}
		protected function progress_handler(event:ProgressEvent):void
		{
			var ratio:Number = event.bytesLoaded / event.bytesTotal;
			_data = int(ratio * 10000) / 100;
			dispatchEvent(new Event("progress"));
		}
		protected function securityError_handler(event:SecurityErrorEvent):void
		{
			_isLoading = false;
			_data = event.text;
			dispatchEvent(new Event("error"));
		}
	}
}